<?php
namespace \Absidon\Controller\Uri;
/**
 * Defines all of the methods in the URI classes.
 *
 * @author Jacob Santos <webmaster@absidongames.com>
 * @since 0.1
 * @package Absidon
 * @subpackage Controller_Uri
 * @license Simplified BSD License <http://www.opensource.org/licenses/bsd-license.html>
 * @license GNU Lesser General Public License 2.1 or later <http://www.opensource.org/licenses/lgpl-2.1.php>
 */

/**
 * URI component to tell the controller what is available.
 */
interface Uri_Component
{
	/**
	 * Whether the URI is using this URI implemented class.
	 *
	 * This method is called on auto setup to pick the correct class. If the class is manually set,
	 * then this method should not be called.
	 *
	 * @return bool true if used, false if not used.
	 */
	public function is_used();

	/**
	 * Process the URI, if needed in order do matching later.
	 *
	 * Will be called as part of the process for all of the URI classes.
	 *
	 * @return null
	 */
	public function process_uri();

}

/**
 * Retrieve URI even on servers that mangle or don't include URI Request.
 *
 * Please note that I'm not going to test this function on http servers other than Apache and IIS 6
 * and 7 (depending on availability). Other servers won't be tested.
 *
 * Some parts of this code were taken from different sources. References were given.
 *
 * @todo Test on IIS 6 and 7 using Mod-Rewrite.
 * @todo Test on IIS 6 and 7 using ISAPI_Rewrite.
 * @todo Test on IIS 7 using Rewrite module.
 * @todo Test with CGI/Fast-CGI.
 * @todo Test with Apache
 *
 * @return string Fixed URI string.
 */
function get_uri_request()
{
	$uri = $_SERVER['URI_REQUEST'];

	if( ! empty($uri) )
	{
		// Add back the query vars to Request URI for IIS 7 that hasn't been updated with the hotfix.
		// http://support.microsoft.com/kb/954946
		if( ! empty($_SERVER['QUERY_STRING']) && strpos('?', $uri) === false )
		{
			return $uri . '?' . $_SERVER['QUERY_STRING'];
		} else {
			// The IIS 7 rewrite module uses REQUEST_URI, but it also has HTTP_X_ORIGINAL_URL, so
			// we have to break out of the function before the IIS Mod_Rewrite fix is applied.
			// http://learn.iis.net/page.aspx/465/url-rewrite-module-configuration-reference/
			return $uri;
		}
	}

	// Fix for IIS Mod_Rewrite
	// http://www.micronovae.com/ModRewrite/articles/CleanURLsDrupal.html
	// http://www.micronovae.com/ModRewrite/articles/CleanPermalinksWordpress.html
	if( isset($_SERVER['HTTP_X_ORIGINAL_URL']) )
	{
		return $_SERVER['HTTP_X_ORIGINAL_URL'];
	}

	// Fix for IIS ISAPI_Rewrite
	// http://www.isapirewrite.com/docs/ See Main Concept
	if( isset($_SERVER['HTTP_X_REWRITE_URL']) )
	{
		return $_SERVER['HTTP_X_REWRITE_URL'];
	}

	// Per the PHP.net comments on REQUEST_URI, checking for empty and use PATH_INFO. Please note,
	// that this does not cover the several cases where PATH_INFO could be empty as well or
	// PATH_INFO isn't even used.
	if( empty($uri) )
	{
		// Fix PATH_INFO, if it is empty, by using the ORIG_PATH_INFO.
		if( empty($_SERVER['PATH_INFO']) && ! empty($_SERVER['ORIG_PATH_INFO']) )
		{
			$_SERVER['PATH_INFO'] = $_SERVER['ORIG_PATH_INFO'];
		}

		// Add script name back to path info if missing.
		if( $_SERVER['SCRIPT_NAME'] != $_SERVER['PATH_INFO'] )
		{
			// The script name contains the script from the url and the path info contains the
			// pretty URI, which is appended to script name. In some cases, the PHP_INFO will not
			// contain the script name and must be added back.
			// http://php.net/manual/en/reserved.variables.server.php
			$_SERVER['PATH_INFO'] = $_SERVER['SCRIPT_NAME'] . $_SERVER['PATH_INFO'];
		}

		$uri = $_SERVER['PATH_INFO'];

		if( isset($_SERVER['argv']) ) // Drupal Fix: http://drupal.org/node/87310
		{
			$uri .= '?' . $_SERVER['argv'][0];
		}
		else if( ! empty($_SERVER['QUERY_STRING']) )
		{
			$uri .= '?' . $_SERVER['QUERY_STRING'];
		}
	}

	return $uri;
}